package com.earl.eatbao.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.earl.eatbao.commons.vo.ResultMessage;
import com.earl.eatbao.domain.eatbao.Address;
import com.earl.eatbao.service.AddressService;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author Administrator
 * @version 1.0
 * @created 23-十一月-2016 16:55:14
 */
@RestController
@RequestMapping("/address")
public class AddressController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    private AddressService addressService;

    /**
     * 查询所有用户地址
     */
   @ApiOperation(value = "查询所有用户地址", notes = "查询所有用户地址", httpMethod = "GET", response = String.class)
   @RequestMapping(value="/all",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findAll() {
        ResultMessage resultMessage = new ResultMessage();

        Iterable<Address> findAll = addressService.findAll();
        resultMessage.getResultParm().put("addressList", findAll);
        return new ResponseEntity<ResultMessage>(resultMessage, HttpStatus.OK);
    }

    /**
     * 查找指定用户的默认地址
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "查找指定用户的默认地址", notes = "查找指定用户的默认地址", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/defalut/{userId}",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findUserDefaultAddress(
            @ApiParam(name = "userId", value = "查找指定用户的默认地址", required = true)
            @PathVariable
            Long userId) {
        ResultMessage resultMessage = new ResultMessage();

        Address address = addressService.findUserDefaultAddress(userId);
        resultMessage.getResultParm().put("address", address);
        return new ResponseEntity<ResultMessage>(resultMessage, HttpStatus.OK);
    }

    /**
     * 查找指定用户的所有收货地址
     *
     * @param userId
     * @return
     */
   @ApiOperation(value = "查找指定用户的所有收货地址", notes = "查找指定用户的所有收货地址", httpMethod = "GET", response = String.class)
   @RequestMapping(value="/all/{userId}",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findUserAllAddress(
            @ApiParam(name = "userId", value = "查找指定用户的所有收货地址", required = true)
            @PathVariable
            Long userId) {

       logger.info("<====== 进入findUserAllAddress:方法,{[userId] :" + userId + " } ====");

        ResultMessage result = new ResultMessage();
        List<Address> addressList = addressService.findUserAllAddress(userId);
        result.getResultParm().put("addressList", addressList);
        result.setServiceResult(true);

        logger.info("==== 退出findUserAllAddress方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 删除用户收货地址
     *
     * @param addressId
     * @return
     */

    @ApiOperation(value = "删除用户收货地址", notes = "删除用户收货地址", httpMethod = "DELETE", response = String.class)
    @RequestMapping(value="/delete/{addressId}",  method = RequestMethod.DELETE)
    public ResponseEntity<ResultMessage> deleteUserAddress(
            @ApiParam(name = "addressId", value = "删除用户收货地址ID", required = false)
            @PathVariable
            Long addressId) {
        //TODO 这里需要添加 删除的用户地址是否是默认用户地址，这时候需要进一步业务逻辑，指定一个地址为默认地址\
        logger.info("<====== 进入deleteUserAddress:方法,{[addressId] :" + addressId + " } ====");

        addressService.delete(addressId);
        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        result.setServiceResult(true);
        result.setMessageCode(200l);
        result.setResultInfo("请求操作成功");

        logger.info("==== 退出deleteUserAddress方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 改变用户默认搜获地址
     *
     * @param addressId
     * @return
     */
    @ApiOperation(value = "改变用户默认搜获地址", notes = "改变用户默认搜获地址", httpMethod = "POST", response = String.class)
    @RequestMapping(value="/changeDefault/{addressId}",  method = RequestMethod.POST)
    public ResponseEntity<ResultMessage> changeUserDefaultAddress(
            @ApiParam(name = "addressId", value = "改变用户默认搜获地址ID", required = true)
            @PathVariable
            Long addressId) {
        //TODO 这里需要添加 删除的用户地址是否是默认用户地址，这时候需要进一步业务逻辑，指定一个地址为默认地址
        // desc: 封装操作结果
        logger.info("<====== 进入changeUserDefaultAddress:方法,{[addressId] : "
                + addressId + " } ====");
        addressService.changeUserDefaultAddress(addressId);
        ResultMessage result = new ResultMessage();
        result.setServiceResult(true);
        result.setMessageCode(200l);
        result.setResultInfo("请求操作成功");

        logger.info("==== 退出changeUserDefaultAddress方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);

    }

    /**
     * 添加收货地址
     *
     * @param address
     * @return
     */
    @ApiImplicitParams({
                @ApiImplicitParam(name = "addressName",value = "地址名字", required = true, dataType = "String", paramType = "Query"),
                @ApiImplicitParam(name = "latitude",value = "纬度", required = true, dataType = "double", paramType = "Query"),
                @ApiImplicitParam(name = "longitude",value = "经度", required = true, dataType = "double", paramType = "Query"),
                @ApiImplicitParam(name = "userId",value = "用户ID", required = true, dataType = "Long", paramType = "Query"),
                @ApiImplicitParam(name = "phone",value = "手机号", required = true, dataType = "String", paramType = "Query"),
                @ApiImplicitParam(name = "name", value = "收货人姓名",required = true, dataType = "String", paramType = "Query")})
    @ApiOperation(value = "添加收货地址", notes = "添加收货地址", httpMethod = "POST", response = String.class)
    @RequestMapping(value="/add",  method = RequestMethod.POST)
    public ResponseEntity<ResultMessage> addAddress(
            @ApiParam(name = "[address]", value = "添加收货地址类封装", required = false)
            Address address) {

        logger.info("<====== 进入addAddress:方法,{[address] :" + address.toString() + "} ====");

        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        if (null != address &&
                address.getAddressName() != null &&
                address.getUserId() != null &&
                address.getPhone() != null) {
           addressService.addAddress(address);
            result.setServiceResult(true);
            result.setResultInfo("请求操作成功");
        } else {
            result.setServiceResult(false);
            result.setResultInfo("请求操作不成");
        }
        result.setMessageCode(200l);

        logger.info("==== 退出addAddress方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 修改收货地址
     *
     * @param address
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressName",value = "地址名字", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "latitude",value = "纬度", required = true, dataType = "double", paramType = "Query"),
            @ApiImplicitParam(name = "longitude",value = "经度", required = true, dataType = "double", paramType = "Query"),
            @ApiImplicitParam(name = "userId",value = "用户ID", required = true, dataType = "Long", paramType = "Query"),
            @ApiImplicitParam(name = "phone",value = "手机号", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = "收货人姓名",required = true, dataType = "String", paramType = "Query")})
    @ApiOperation(value = "修改收货地址", notes = "修改收货地址", httpMethod = "POST", response = String.class)
    @RequestMapping(value="/update",  method = RequestMethod.POST)
    public ResponseEntity<ResultMessage> updateAddress(
            @ApiParam(name = "[address]", value = "修改收货地址", required = true)
            @RequestParam(name = "address", required = true) Address address) {

        logger.info("<====== 进入updataAddress:方法,{[address] : } ====");

        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        if (null != address &&
                address.getUserId() != null &&
                address.getAddressName() != null) {
            addressService.save(address);
            result.setServiceResult(true);
            result.setResultInfo("请求操作成功");
        } else {
            result.setServiceResult(false);
            result.setResultInfo("请求操作不成");
        }
        result.setMessageCode(200l);

        logger.info("==== 退出updataAddress方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }
}